package com.rankeiot.platform.controller;

import com.rankeiot.core.data.response.Resp;
import com.rankeiot.core.util.LocaleUtil;
import com.rankeiot.core.util.StringUtil;
import com.rankeiot.platform.domain.TDictionary;

import com.rankeiot.platform.service.CacheDataService;
import com.rankeiot.core.anno.Auth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* 字典管理
*
* generated by palmboot
**/
@Tag(name = "字典管理")
@Auth
@RestController
@RequestMapping("dictionary")
@RequiredArgsConstructor
public class DictionaryController{
    final CacheDataService cacheDataService;
    final SqlToyLazyDao dao;

    @Operation(summary="新增字典管理")
    @PostMapping("add")
    public Resp add(@Valid @RequestBody TDictionary dictionary){
        if (StringUtil.isEmpty(dictionary.getType())){
            dictionary.setType("dict");
        }
        dictionary.setUpdateTime(new Date());
        dao.save(dictionary);
        //重建字典缓存
        cacheDataService.buildDictCache();
        return Resp.ok();
    }

    @Operation(summary="删除字典管理")
    @PostMapping("delete")
    @Transactional
    public Resp delete(@RequestBody List<TDictionary> dictionarys){
        // dao.loadAll(dictionarys);
        if(dictionarys.size()==0){
            return Resp.ok();
        }
        List<TDictionary> toDeletes= dao.loadAll(dictionarys);
        List<TDictionary> subDicts=new ArrayList<>();
        for(TDictionary dict:toDeletes){
            if("dict".equals(dict.getType())){//字典类型级别，需要删除其子级
                subDicts.addAll(dao.findBySql("select * from t_dictionary where `type`=:sn",dict));
            }
        }
        toDeletes.addAll(subDicts);
        dao.deleteAll(toDeletes);
        //重建字典缓存
        cacheDataService.buildDictCache();
        return Resp.ok();
    }

    @Operation(summary="更新字典管理")
    @PostMapping("save")
    public Resp save(@Valid @RequestBody TDictionary dictionary){
        dictionary.setUpdateTime(new Date());
        dao.update(dictionary);
        //重建字典缓存
        cacheDataService.buildDictCache();
        return Resp.ok();
    }

    @Operation(summary="获取字典管理")
    @GetMapping("detail")
    public Resp<TDictionary> detail(TDictionary query){
       TDictionary dictionary=dao.load(query);
       return Resp.of(dictionary);
    }

    @Operation(summary="获取字典管理列表")
    @PostMapping("list")
    public Resp<List<TDictionary>> list(@RequestBody TDictionary query){
        if(StringUtil.isEmpty(query.getType())){
            return Resp.of(new ArrayList<>());
        }
        List<TDictionary> result = dao.findBySql("select * from t_dictionary where #[and type=:type] #[and ( name like :name or sn like :name)] order by `rank`",query);
        Locale locale= LocaleUtil.getLocale();
        for(TDictionary dictionary:result){
            dictionary.setName(LocaleUtil.d(dictionary.getName(),locale));
        }
        return Resp.of(result);
    }

}
